package edu.uic.cs.i422.s2011.team4;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SiteListing extends Activity {

	public ArrayList<SquirrelData> sharedData;
	public ArrayList<String> siteNames;
	int currentPosition;
	private ListView siteListView;
	private LinearLayout siteListLayout;
	private int _position = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Button deleteButton;
		final Button viewButton;
		final Button selectButton;
		final Button gpsButton;

		/**
		 * Get our UI Generator for this Activity and set the title, help, and
		 * tree Note that the strings should be R.string....
		 */
		SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
		squirrel.setTitle(R.string.siteListingTitle);
		squirrel.setHelp(R.string.siteListingHelp);
		squirrel.setBreadCrumb(R.string.siteListingBreadCrumb);

		// Retrieve shared squirrel data from res file

		// Retrieve layout

		/**
		 * Create the encapsulating layout for this activity. We are going to
		 * add every View we create here using addView().
		 */
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL); // Set Orientation
		layout.setScrollContainer(true);

		/**
		 * Bring forth my setting template
		 */
		LinearLayout listLayout = (LinearLayout) View.inflate(this,
				R.layout.sitelist, null);
		layout.addView(squirrel.getHeader());
		layout.addView(listLayout);

		setContentView(layout);
		_position = SquirrelData.selectedIndex + 1;
		siteListView = (ListView) findViewById(R.id.listing);
		siteListView.setTextFilterEnabled(true);
		siteListView.setSelected(true);
		siteListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		siteListView.setLongClickable(true);

		ArrayList<String> newList = new ArrayList<String>();
		newList.add("Add New Site");
		siteListView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, SquirrelData
						.getSiteNames(newList)));

		siteListView.setItemChecked(SquirrelData.selectedIndex + 1, true);

		// Listener for the list view

	//	siteListView.setOnLongClickListener(new View.OnLongClickListener() {

//		public boolean onLongClick(final View v) {
	

//		}

		/**
		 * Make a button and add click listeners
		 */
		selectButton = (Button) findViewById(R.id.selectSite);

		selectButton.setOnClickListener(new OnClickListener() {

			public void onClick(final View v) {
				AlertDialog.Builder builder;
				AlertDialog dialog;
				Context context = v.getContext();
				final LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				final LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.site_listing_dialog,
				(ViewGroup) findViewById(R.id.siteDialog));
		 
				
				
				final EditText siteName = (EditText) layout.findViewById(R.id.geocode_site);
				final EditText siteZipCode = (EditText) layout.findViewById(R.id.zip_code);
				final EditText siteLat = (EditText) layout.findViewById(R.id.lat_input);
				final EditText siteLon = (EditText) layout.findViewById(R.id.lon_input);
			//	final Button siteDialogButton = (Button) layout.findViewById(R.id.presentLocation_button);
	//			final Button siteDialogButton = (Button) inflater.inflate(R.layout.site_listing_dialog,(ViewGroup) findViewById(R.id.presentLocation_button));

//		final Button gpsButton = (Button) findViewById(R.id.site_gps);
		//		final Geocoder geocoder = new Geocoder(v.getContext());
				


				if (_position == 0) {

					builder = new AlertDialog.Builder(v.getContext());
					builder.setTitle("New Observation Site");
					builder.setView(layout);
					
					// Update location
		//			siteDialogButton.setOnClickListener(new OnClickListener() {
		//				public void onClick(View v) {
						//	lattitude.setText("Lattitude: " + SquirrelData.xCurrent);
						//	longitude.setText("Longitude: " + SquirrelData.yCurrent);
		//				}
		//		});
					siteLat.setText(Double.toString(SquirrelData.lat));
					siteLon.setText(Double.toString(SquirrelData.lat));
					
					builder.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog,
										int whichButton) {

									String name = siteName.getText().toString();
									String zip_code = siteZipCode.getText().toString();
									String lat = siteLat.getText().toString();
									String lon = siteLon.getText().toString();
									Double latDouble = Double.parseDouble(lat);
									Double lonDouble = Double.parseDouble(lon);
									AlertDialog.Builder builder;
									AlertDialog alertDialog;
									Context context = v.getContext();
									
									
									
									if (name.length() == 0) {
										builder = new AlertDialog.Builder(
												context);
										builder.setMessage("Please insert a site name");

										builder.setPositiveButton("OK", null);
										alertDialog = builder.create();
										alertDialog.show();
										return;
									}
										
									if (lat.length() == 0 || lon.length() == 0) {
										builder = new AlertDialog.Builder(
												context);
										builder.setMessage("Please insert lat/long info");

										builder.setPositiveButton("OK", null);
										alertDialog = builder.create();
										alertDialog.show();
										return;
									}
									
									
									if (!zip_code.matches("[0-9][0-9][0-9][0-9][0-9]")) {
										

										builder = new AlertDialog.Builder(
												context);
										builder.setMessage("Zip code must contain 5 numbers" + zip_code);

										builder.setPositiveButton("OK", null);
										alertDialog = builder.create();
										alertDialog.show();
										return;
									}
									
									if (!SquirrelData.getSiteNames().contains(
											name)) {
										SquirrelData.lat = latDouble;
										SquirrelData.lon = lonDouble;
										SquirrelData.addNewSquirrrelSite(name,Integer.parseInt(zip_code));
										Intent intent = new Intent(v
												.getContext(),
												SettingObservation.class);
										startActivityForResult(intent, 0);
										finish();
									} else {

										builder = new AlertDialog.Builder(
												context);
										builder.setTitle("Duplicate Site Name");
										builder.setMessage("A site with that name already exists.");

										builder.setPositiveButton("OK", null);
										alertDialog = builder.create();
										alertDialog.show();
									}
								}
							});
					
					

					builder.setNegativeButton("CANCEL", null);
					dialog = builder.create();
					dialog.show();

				} else {
					Intent intent = new Intent(v.getContext(), Form.class);
					startActivityForResult(intent, 0);
					finish();
				}
			}

		});

		

		deleteButton = (Button) findViewById(R.id.deleteSiteButton);
		if (SquirrelData.selectedIndex == -1)
			deleteButton.setEnabled(false);
		else
			deleteButton.setEnabled(true);
		deleteButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				SquirrelData.remove(SquirrelData.selectedIndex);
				Intent intent = new Intent(v.getContext(), SiteListing.class);
				startActivityForResult(intent, 0);
				finish();
			}
		});

		viewButton = (Button) findViewById(R.id.viewMapButton);
		if (SquirrelData.selectedIndex == -1)
			viewButton.setEnabled(false);
		else
			viewButton.setEnabled(true);

		viewButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), viewMap.class);
				startActivityForResult(intent, 0);
				finish();
			}
		});

		siteListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					deleteButton.setEnabled(false);
					viewButton.setEnabled(false);
				} else {
					viewButton.setEnabled(true);
					deleteButton.setEnabled(true);
					SquirrelData.setSelectedIndex(position - 1);
				}
				_position = position;
			}

		});

	}

}
/*
 * btnSimple = (Button) findViewById(R.id.newSiteButton);
 * btnSimple.setOnClickListener(new OnClickListener() {
 * 
 * public void onClick(final View v) { AlertDialog.Builder builder; AlertDialog
 * dialog; Context context = v.getContext(); final LayoutInflater inflater =
 * (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 * 
 * final LinearLayout layout = (LinearLayout)
 * inflater.inflate(R.layout.site_listing_dialog, (ViewGroup)
 * findViewById(R.id.site_root)); final EditText editLayout = (EditText)
 * layout.findViewById(R.id.siteEditName); builder = new
 * AlertDialog.Builder(v.getContext());
 * builder.setTitle("New Observation Site"); builder.setView(layout);
 * 
 * builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
 * public void onClick(DialogInterface dialog, int whichButton) {
 * 
 * 
 * SquirrelData.addNewSquirrrelSite(editLayout.getText().toString()); Intent
 * intent = new Intent(v.getContext(),TreeObservation.class);
 * startActivityForResult(intent,0); finish(); } });
 * 
 * builder.setNegativeButton("CANCEL",null); dialog = builder.create();
 * dialog.show(); }
 * 
 * }); }
 */

